import 'package:example/app/data/model/article_bean.dart';
import 'package:example/app/data/model/banner_bean.dart';
import 'package:example/app/data/model/hot_keywords_bean.dart';
import 'package:example/app/data/model/login_bean.dart';
import 'package:example/app/data/model/page_bean.dart';

import '../provider/wan_android_api.dart';

class WanAndroidRepository {
  /// 登录
  /// [Http._init] 添加了拦截器 设置了自动cookie.
  static Future login(String username, String password) async {
    var response = await http.post<Map>('/user/login', queryParameters: {
      'username': username,
      'password': password,
    });
    return LoginBean.fromJson(response.data as Map<String, dynamic>);
  }

  /// 获取BannerList
  static Future getBanner() async {
    var response = await http.get<List>('/banner/json');
    return response.data?.map((e) => BannerBean.fromJson(e)).toList();
  }

  /// 获取置顶文章列表
  static Future getTopArticleList() async {
    var response = await http.get<List>('/article/top/json');
    return response.data?.map((e) => ArticleBean.fromJson(e)).toList();
  }

  /// 获取文章列表
  static Future<PageBean<ArticleBean>> getArticleList({required int page}) async {
    var response = await http.get('/article/list/$page/json');
    return PageBean.fromJson(response.data! as Map<String, dynamic>,
        (val) => ArticleBean.fromJson(val));
  }

  // 收藏
  static collect(id) async {
    await http.post('/lg/collect/$id/json');
  }

  // 取消收藏 文章列表
  static unCollect(id) async {
    await http.post('/lg/uncollect_originId/$id/json');
  }

  // 取消收藏2 我的收藏
  static unMyCollect({id, originId}) async {
    await http.post('/lg/uncollect/$id/json',
        queryParameters: {'originId': originId ?? -1});
  }

  /// 获取广场列表
  static Future<PageBean<ArticleBean>> getSquareArticleList({required int page}) async {
    var response = await http.get('/user_article/list/$page/json');
    return PageBean.fromJson(response.data! as Map<String, dynamic>,
            (val) => ArticleBean.fromJson(val));
  }

  /// 问答列表
  static Future<PageBean<ArticleBean>> getQaArticleList({required int page}) async {
    var response = await http.get('/wenda/list/$page/json');
    return PageBean.fromJson(response.data! as Map<String, dynamic>,
            (val) => ArticleBean.fromJson(val));
  }

  /// 获取搜索热词
  static Future getHotkey()async{
    var response = await http.get<List>('/hotkey/json');
    return response.data?.map((e) => HotKeywordsBean.fromJson(e)).toList();
  }

  /// 搜索文章列表
  static Future queryArticle({required int page, required String keywords}) async{
    var response = await http.post('/article/query/$page/json', queryParameters: {'k': keywords});
    return PageBean.fromJson(response.data! as Map<String, dynamic>,
            (val) => ArticleBean.fromJson(val));
  }
}
